package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.RemoveInfo;
import gatperdut.chato.core.item.ItemController;

public class RemoveAction extends Action {

    public RemoveAction( RemoveInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        if ( emptyEmote() ) {
            getEntityController().tx( "Empty emotes are not allowed." );
            return CommandExecutionResult.OK;
        }

        String emote = getLastPartEmote();
        if ( emote == null ) {
            getEntityController().tx( "Check your emote." );
            return CommandExecutionResult.OK;
        }

        if ( !checkUnindexedWord( getInfo().getWhat() ) ) {
            return CommandExecutionResult.OK;
        }
        ItemController ic = getEntityController().getInventoryController().getItemController( getInfo().getWhat() );
        if ( ic == null || ic == getEntityController().getRHandItemController() || ic == getEntityController().getLHandItemController() ) {
            getEntityController().tx( "You don't seem to be wearing that." );
            return CommandExecutionResult.OK;
        }

        if ( !getEntityController().hasOneFreeHand() ) {
            getEntityController().tx( "You'll need a free hand to do that." );
            return CommandExecutionResult.OK;
        }

        getEntityController().getInventoryController().takeFromInventory( ic );

        getEntityController().getInventoryController().take( ic );

        getEntityController().getEchoes().txRemove( ic, emote );

        return CommandExecutionResult.OK;

    }

    @Override
    public RemoveInfo getInfo() {
        return (RemoveInfo) super.getInfo();
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

}
